package cz.incad.psh.pshassign.server.rpc;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import cz.incad.psh.oai_solr.Converter;
import cz.incad.psh.oai_solr.ProgramArguments;
import cz.incad.psh.psh_solr.PSHtoSolr;
import cz.incad.psh.pshassign.client.rpc.PSHService;
import cz.incad.psh.pshassign.server.Assigner;
import cz.incad.psh.pshassign.server.DocAssigner;
import cz.incad.psh.pshassign.server.Institutions;
import cz.incad.psh.pshassign.server.Invenio;
import cz.incad.psh.pshassign.server.Options;
import cz.incad.psh.pshassign.server.PSHNames;
import cz.incad.psh.pshassign.server.StopWords;
import cz.incad.psh.pshassign.server.search.NuslSearch;
import cz.incad.psh.pshassign.server.search.Search;
import cz.incad.psh.pshassign.server.search.VectorsSearch;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.logging.Level;
import java.util.logging.Logger;

public class PSHServiceImpl extends RemoteServiceServlet implements PSHService {

    @Override
    public String searchNusl(String q) {
        Search search = new NuslSearch();
        return search.doSearch(q);
    }

    @Override
    public String getPSHKeys(String id) {
        
        try{
            DocAssigner a = new DocAssigner(id);
            return a.asJson();
        }catch(Exception ex){
            ex.printStackTrace();
            return "{\"error\":\""+ex.getLocalizedMessage()+"\" }";
        }
//        VectorsSearch vs = new VectorsSearch();
//        String json = vs.doSearch(id);
//        JSONObject tvhrJson  = new JSONObject(json);
//        PSHSearch search = new PSHSearch();
//        return search.doSearch(tvhrJson);
    }

    @Override
    public String getDocVectors(String id) {
        Search search = new VectorsSearch();
        return search.doSearch(id);
    }

    @Override
    public String getOptions() throws Exception {
        return Options.load().toString();

    }

    @Override
    public void setOptions(String props) throws IOException {
        Options.save(props);

    }

    @Override
    public ArrayList<String> getStopWords() throws Exception {
        return StopWords.load();

    }

    @Override
    public void addStopWord(String word) throws Exception {
        StopWords.add(word);

    }

    @Override
    public void removeStopWord(String word) throws Exception {
        StopWords.remove(word);
    }

    @Override
    public String getKeyNames(String ids) throws Exception {
        PSHNames search = new PSHNames();
        return search.getNames(ids);
    }
    
    /**
     *
     */
    @Override
    public void cancelIndex(){
        Converter.setCancelled(true);
    }

    @Override
    public int getCurrentIndex() throws Exception {
        return Converter.getProccessed();
    }
    

    @Override
    public boolean getIsRunningIndex() {
        return Converter.getIsRunning();
    }

    @Override
    public String fullIndex() throws Exception {
        try {
            ArrayList<String> a = new ArrayList<String>();
            //-orig http://aleph.techlib.cz/OAI 
            // -xsl ./src/nusl.xsl -dest http://localhost:8983/solr/nusl 
            // -set CGL -metadataPrefix marc21
            a.add("-orig");
            a.add(Options.getOption("invenio", "oai_url"));
            a.add("-xsl");
            a.add(Options.getOption("invenio", "oai_xsl"));
            a.add("-dest");
            a.add(Options.getOption("invenio", "solr"));
            a.add("-set");
            a.add(Options.getOption("invenio", "oai_set"));
            a.add("-metadataPrefix");
            a.add(Options.getOption("invenio", "oai_metadataPrefix"));
            a.add("-clear");

            String[] args = a.toArray(new String[a.size()]);
            ProgramArguments arguments = new ProgramArguments();
            if (!arguments.parse(args)) {
                return "Program arguments are invalid: " + Arrays.toString(args);
            }
            Converter converter = new Converter(arguments);
            return converter.convert();
        } catch (Exception ex) {
            System.out.println(ex);
            throw new Exception(ex);
        }
    }

    @Override
    public String updateIndex(String from) throws Exception {
        
        try {
            ArrayList<String> a = new ArrayList<String>();
            a.add("-orig");
            a.add(Options.getOption("invenio", "oai_url"));
            a.add("-xsl");
            a.add(Options.getOption("invenio", "oai_xsl"));
            a.add("-dest");
            a.add(Options.getOption("invenio", "solr"));
            a.add("-set");
            a.add(Options.getOption("invenio", "oai_set"));
            a.add("-metadataPrefix");
            a.add(Options.getOption("invenio", "oai_metadataPrefix"));
            a.add("-from");
            a.add(from);

            String[] args = a.toArray(new String[a.size()]);
            ProgramArguments arguments = new ProgramArguments();
            if (!arguments.parse(args)) {
                return "Program arguments are invalid: " + Arrays.toString(args);
            }
            Converter converter = new Converter(arguments);
            return converter.convert();
        } catch (Exception ex) {
            System.out.println(ex);
            throw new Exception(ex);
        }
    }

    @Override
    public String indexPsh() throws Exception {
        try {
            ArrayList<String> a = new ArrayList<String>();
            a.add("-xsl");
            a.add(Options.getOption("psh", "xsl"));
            a.add("-solr");
            a.add(Options.getOption("psh", "solr"));
            a.add("-rdf");
            a.add(Options.getOption("psh", "rdf"));
            a.add("-clear");

            String[] args = a.toArray(new String[a.size()]);
            PSHtoSolr psh = new PSHtoSolr(); 
            psh.run(args);
            return "success";
        } catch (Exception ex) {
            System.out.println(ex);
            throw new Exception(ex);
        }
    }

    @Override
    public String updatePsh(String url) throws Exception {
        try {
            ArrayList<String> a = new ArrayList<String>();
            a.add("-xsl");
            a.add(Options.getOption("psh", "xsl"));
            a.add("-solr");
            a.add(Options.getOption("psh", "solr"));
            a.add("-rdf");
            a.add(url);

            String[] args = a.toArray(new String[a.size()]);
            PSHtoSolr psh = new PSHtoSolr(); 
            psh.run(args);
            return "success";
        } catch (Exception ex) {
            System.out.println(ex);
            throw new Exception(ex);
        }
    }

    @Override
    public String assign(boolean update) throws Exception {
        if(Assigner.isRunning){
            return "already running";
        }else{
            Assigner a =  new Assigner();
            a.assign(update);
            return "success";
        }
    }

    @Override
    public int getCurrentAssign() throws Exception {
        return Assigner.getProccessed();
    }
    

    @Override
    public boolean getIsRunningAssign() {
        return Assigner.getIsRunning();
    }
    
    @Override
    public void cancelAssign(){
        Assigner.setCancelled(true);
    }

    @Override
    public String saveToInvenio(String xml) {
        Invenio i = new Invenio();
        try {
            return i.update(xml);
        } catch (Exception ex) {
            Logger.getLogger(PSHServiceImpl.class.getName()).log(Level.SEVERE, null, ex);
            return "error";
        }
    }

    @Override
    public String getInstitutions() throws Exception {
        return Institutions.load().toString();

    }

    @Override
    public void setInstitutions(String json) throws IOException {
        Institutions.save(json);

    }
}
